package com.cloudbroker.openapi.controller;

import static com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.CommonErr.FILE_TRANS_ERROR;
import static com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.CommonErr.FILE_USEFULL_ITEM_EMPTY;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSFields;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.back.BatchImportBrokerBackForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.BatchImportBrokerForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.BatchImportBrokerListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.BatchImportVoteForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.BatchImportVoteListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.BatchImportZXConfigForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.customer.vo.BatchImportZXConfigListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.upvote.back.BatchImportVoteBackForm;
import com.cloudbroker.bcs.platform.dav.bizapi.t2service.business.BatchImportT2BizService;
import com.cloudbroker.bcs.platform.dav.common.ext.annotation.SaveLog;
import com.cloudbroker.bcs.platform.dav.common.util.ImportExcel;

/**
 * Created by zhaojp18008 on 2016/10/25.
 */
@Controller
public class BatchImportController {
    @Resource
    private BatchImportT2BizService batchImportT2;
    
    /**
     * 批量导入投顾注册
     */
    @ResponseBody
    @RequestMapping("/func_batch_import_broker")
    @SaveLog(remark = "批量导入投顾注册")
    public BatchImportBrokerBackForm funcBatchImportBroker(MultipartHttpServletRequest request) {
        try {
            MultipartFile multipartFile = request.getFile(HSBCPSFields.FILE);
            ImportExcel importExcel = new ImportExcel(multipartFile, 1, 0);
            List<BatchImportBrokerForm> brokerFormList = importExcel.getDataList(BatchImportBrokerForm.class);
            if (brokerFormList == null || brokerFormList.size() == 0) {
                return VOUtil.genErrorResult(FILE_USEFULL_ITEM_EMPTY, BatchImportBrokerBackForm.class);
            }
            BatchImportBrokerListForm batchImportListForm = new BatchImportBrokerListForm();
            batchImportListForm.setDataList(brokerFormList);
            return VOUtil.setJsonpResult(batchImportT2.funcBatchImportBroker(batchImportListForm), null);
        } catch (IOException | InvalidFormatException | InstantiationException | IllegalAccessException e) {
            return VOUtil.genErrorResult(FILE_TRANS_ERROR, BatchImportBrokerBackForm.class);
        }
        
    }
    
    /**
     * 批量导入投票
     */
    @ResponseBody
    @RequestMapping("/func_batch_import_vote")
    @SaveLog(remark = "批量导入投票")
    public BatchImportVoteBackForm funcBatchImportVote(MultipartHttpServletRequest request) {
        MultipartFile multipartFile = request.getFile(HSBCPSFields.FILE);
        BatchImportVoteListForm batchImportListForm = new BatchImportVoteListForm();
        try {
            List<BatchImportVoteForm> batchImportZXConfigForms = getListFromMultipartFile(multipartFile,
                    BatchImportVoteForm.class);
            if (batchImportZXConfigForms == null || batchImportZXConfigForms.size() == 0) {
                return VOUtil.genErrorResult(FILE_USEFULL_ITEM_EMPTY, BatchImportVoteBackForm.class);
            }
            batchImportListForm.setDataList(batchImportZXConfigForms);
            
            return VOUtil.setJsonpResult(batchImportT2.funcBatchImportVote(batchImportListForm), null);
        } catch (Exception e) {
            return VOUtil.genErrorResult(FILE_TRANS_ERROR, BatchImportVoteBackForm.class);
        }
    }
    
    /**
     * 批量导入投顾配置
     */
    @ResponseBody
    @RequestMapping("/func_batch_import_finance_config")
    @SaveLog(remark = "批量导入投顾配置")
    public BatchImportVoteBackForm funcBatchImportFinanceConfig(MultipartHttpServletRequest request) {
        MultipartFile multipartFile = request.getFile(HSBCPSFields.FILE);
        BatchImportZXConfigListForm batchImportListForm = new BatchImportZXConfigListForm();
        try {
            List<BatchImportZXConfigForm> batchImportZXConfigForms = getListFromMultipartFile(multipartFile,
                    BatchImportZXConfigForm.class);
            if (batchImportZXConfigForms == null || batchImportZXConfigForms.size() == 0) {
                return VOUtil.genErrorResult(FILE_USEFULL_ITEM_EMPTY, BatchImportVoteBackForm.class);
            }
            batchImportListForm.setDataList(batchImportZXConfigForms);
            
            return VOUtil.setJsonpResult(batchImportT2.funcBatchImportFinanceConfig(batchImportListForm), null);
        } catch (Exception e) {
            return VOUtil.genErrorResult(FILE_TRANS_ERROR, BatchImportVoteBackForm.class);
        }
    }
    
    private <T> List<T> getListFromMultipartFile(MultipartFile multipartFile, Class<T> s) throws IOException,
            InvalidFormatException, IllegalAccessException, InstantiationException {
        ImportExcel importExcel = new ImportExcel(multipartFile, 0, 0);
        List<T> voteFormList = importExcel.getDataList(s);
        if (voteFormList == null || voteFormList.size() == 0) {
            return null;
        }
        return voteFormList;
    }
    
}
